/**
 * 大厅界面
 * by wyy
 */
class hallScene extends GameObject implements eui.UIComponent {
    public displayGrp: eui.Group;
    public listGrp: eui.Group;
    public scroller1: eui.Scroller;
    public deskGrp1: eui.Group;
    public scroller2: eui.Scroller;
    public deskGrp2: eui.Group;
    public scroller3: eui.Scroller;
    public deskGrp3: eui.Group;
    public textGrp: eui.Group;
    public broadcastList: eui.List;
    public joinDeskBtn: eui.Button;
    public quitBtn: eui.Button;
    public refreshBtn: eui.Button;
    public nameLbl: eui.Label;
    public ruleBtn: eui.Button;

    public tipLbl: eui.Label;
    public timeLbl: eui.Label;

    public broadcastScro: eui.Scroller;

    type = "hall";

    heightPercent = 1;
    widthPercent = 1;

    constructor() {
        super();
        this.skinName = "resource/eui_skins/game_skins/hallSceneSkin.exml";
    }
    addToStage() {

    }
    firstCreate() {
        globalObject.getInstance().showRank = true;
        this.nameLbl.text = globalObject.getInstance().player.Username;

        this.broadcastList.dataProvider = globalObject.getInstance().resultListArr;
        this.broadcastScro.viewport = this.broadcastList;
        this.broadcastScro.bounces = false;
        this.joinDeskBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.joinDesk, this);
        this.quitBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,
            () => { SceneManager.getInstance().changeScene(new introScene()) }, this);
        this.refreshBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.refreshList, this);

        this.refreshList();
        if (globalObject.getInstance().createHallListener) {
            nano.on("rollBroadst", (d) => {
                if (SceneManager.getInstance().getCurrentScene().type == "hall") {
                    globalObject.getInstance().debugConsole("他人摇骰广播通知", d);
                    (<hallScene>SceneManager.getInstance().getCurrentScene()).showBroadcast(`[第${d.round}轮]来自${d.deskid}桌的`, `${d.user_name}`,
                        `摇了${d.cur_score}点,总成绩为${d.score}点！`);
                }
            });
            nano.on("gameend", (data) => {
                if (SceneManager.getInstance().getCurrentScene().type == "hall") {
                    let scene = (<hallScene>SceneManager.getInstance().getCurrentScene())
                    scene.setTipText(data.message);
                }
            });
            globalObject.getInstance().createHallListener = false;
        }

        //拉取已经投掷的结果
        nano.request("gamemanager.getmsg", {}, (d) => {
            globalObject.getInstance().debugConsole("已投掷记录", d);
            let list = [];
            for (let i in d) {
                list.push(d[i]);
            }
            if (list.length > 30) {
                list = list.slice(-30);
            }
            globalObject.getInstance().resultListArr.removeAll();
            for (let v of list) {
                this.showBroadcast(`[第${v.round}轮]来自${v.deskid}桌的`, `${v.user_name}`,
                    `摇了${v.cur_score}点,总成绩为${v.score}点！`, false);
            }
        });
        this.ruleBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,
            () => { SceneManager.getInstance().showWindow(new helpScene()) }, this);
    }
    setTipText(s) {
        if (s.indexOf("/")) {
            let arr = s.split("/");
            this.timeLbl.text = arr[1];
            this.tipLbl.text = arr[0];
        }
        else {
            this.timeLbl.text = s;
        }
    }
    private refreshList() {
        for (let i = 0; i < 3; i++) {
            let round = 1 + i;
            this["deskGrp" + round].removeChildren();
            nano.request("gamemanager.getdesk", { round: 1 + i }, (data) => {
                if (data.code == 200) {
                    (<eui.Scroller>this["scroller" + round]).verticalScrollBar.autoVisibility = false;
                    (<eui.Scroller>this["scroller" + round]).horizontalScrollBar.autoVisibility = false;
                    (<eui.Scroller>this["scroller" + round]).bounces = false;
                    globalObject.getInstance().debugConsole(`第${Number(i + 1)}轮桌子`, data.desks);
                    this["deskGrp" + round].removeChildren();
                    for (let i in data.desks) {
                        this["deskGrp" + round].addChild(new deskListItem(round, data.desks[i]));
                    }
                }
            });
        }
    }
    private showBroadcast(s1, s2, s3, animation = true) {
        let scene = <hallScene>SceneManager.getInstance().getCurrentScene();
        globalObject.getInstance().resultListArr.addItem({ text1: s1, name: s2, text2: s3 });
        globalObject.getInstance().debugConsole("玩家投掷公告", { s1, s2, s3 });
        scene.broadcastScro.validateNow();
        if (animation) {
            if (scene.broadcastScro.viewport.contentHeight > scene.broadcastScro.viewport.height) {
                scene.broadcastScro.stopAnimation();
                egret.Tween.get(scene.broadcastScro.viewport).to({
                    scrollV: scene.broadcastScro.viewport.contentHeight - scene.broadcastScro.viewport.height
                }, 500);
            }
        }
        else {
            if (this.broadcastScro.viewport.contentHeight > this.broadcastScro.viewport.height) {
                this.broadcastScro.stopAnimation();
                this.broadcastScro.viewport.scrollV = this.broadcastScro.viewport.contentHeight - this.broadcastScro.viewport.height;
            }
        }
    }
    onRemove() { }
    private joinDesk() {
        let id = loginScene.getInstance().playerInfo.id;
        nano.request("gamemanager.joindesk", { id: id }, (data) => {
            globalObject.getInstance().debugConsole("当前桌子信息：", data);
            switch (data.code) {
                //加入成功
                case 200:
                    globalObject.getInstance().showTip(data.message);
                    loginScene.getInstance().playerInfo.deskid = data.data.desk_id;
                    SceneManager.getInstance().changeScene(new deskScene(data));
                    break;
                //其他情况
                default:
                    SceneManager.getInstance().showWindow(
                        new tipWindow({ text: data.message, btnText: "观战" }));
                    break;
            }
        });
    }
}